<template>
  <el-dialog width="800px" :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
             :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
      <el-row>
        <el-col :span="12">
          <el-form-item label="名称" size='small' prop="name">
            <el-input v-model="dataForm.name" placeholder="名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="阶段" size='small' prop="stage">
            <el-select v-model="dataForm.stage" placeholder="请选择">
              <el-option
                  v-for="item in stages"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="最大存栏" size='small' prop="max">
            <el-input v-model="dataForm.max" placeholder="最大存栏"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="栏位类型" size='small' prop="grid">
            <el-select v-model="dataForm.grid" placeholder="请选择">
              <el-option
                  v-for="item in gridTypes"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="行数" size='small' prop="rows">
            <el-input v-model="dataForm.rows" placeholder="行数"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="行起始字符" size='small' prop="rowStart">
            <el-input v-model="dataForm.rowStart" placeholder="行起始字符"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="列数" size='small' prop="columns">
            <el-input v-model="dataForm.columns" placeholder="列数"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="列起始字符" size='small' prop="colStart">
            <el-input v-model="dataForm.colStart" placeholder="列起始字符"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-row>
        <el-col :span="12">
          <el-form-item label="栏位合并" size='small'>
            <el-input v-model="dataForm.rows"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item size='small' v-if="dataForm.rows && dataForm.columns">
            <el-button type="primary" size="mini" icon="el-icon-plus" circle></el-button>
            <el-button type="primary" size="mini" icon="el-icon-check" circle></el-button>
            <el-button type="danger" size="mini" icon="el-icon-close" circle></el-button>
          </el-form-item>
        </el-col>
      </el-row> -->
      <el-form-item size='small' label="备注" prop="remark">
        <el-input type="textarea" :rows="2" v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <el-table-column size='small' prop="remark" label="栏位" header-align="center" align="center">
      <Grid :grid="dataForm"></Grid>
    </el-table-column>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
/* eslint-disable */
import debounce from 'lodash/debounce'
// 1育肥 2保育 3配种 4公猪 5妊娠 6哺乳
// grid 栏位类型 0大栏 1限位栏
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: '',
        name: '',
        stage: '',
        max: '',
        grid: '',
        rows: '',
        columns: '',
        rowStart: '',
        colStart: '',
        gridMerges: [],
        remark: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: ''
      },
      gridTypes: [{
        value: 0,
        label: '大栏'
      }, {
        value: 1,
        label: '限位栏'
      }],
      stages: [{
        value: 1,
        label: '育肥'
      }, {
        value: 2,
        label: '保育'
      }, {
        value: 3,
        label: '配种'
      }, {
        value: 4,
        label: '公猪'
      }, {
        value: 5,
        label: '妊娠'
      }, {
        value: 6,
        label: '哺乳'
      }]
    }
  },
  computed: {
    dataRule() {
      return {
        name: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        stage: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        max: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        grid: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        rows: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        columns: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/basic/house/${this.dataForm.id}`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/basic/house/', this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {
        })
      })
    }, 1000, {'leading': true, 'trailing': false})
  }
}
</script>
<style scoped>
    .el-input, .el-select {
      width: 180px;
    }

    .el-textarea {
      width: 460px;
    }
</style>
